package com.crm.system.service.impl.user;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.UserInfo;
import com.crm.model.entity.sys.UserInfoSubordinate;
import com.crm.model.vo.user.UserInfoVO;
import com.crm.service.sys.UserAccountDbService;
import com.crm.service.sys.UserInfoDbService;
import com.crm.service.sys.UserInfoSubordinateDbService;
import com.crm.system.service.user.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/3/16 13:51
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoDbService userInfoDbService;

    @Autowired
    private UserInfoSubordinateDbService userInfoSubordinateDbService;

    @Autowired
    private UserAccountDbService userAccountDbService;

    @Override
    public UserInfo selectUserInfoByPrivateKey(String userId) {
        UserInfo  userInfo =userInfoDbService.selectUserInfoByUserId(userId);
        return userInfo;
    }

    /**
     * 通过userId查询个人信息
     * @param userId 用户id
     * @return
     */
    @Override
    public UserInfoVO getUserInfo(String userId) {
        return userInfoDbService.getUserInfo(userId);
    }

    /**
     * 个人信息-保存图片    没有userId就新增  有就修改
     * @param userInfo 用户基本信息实体
     * @return
     */
    @Override
    public Boolean saveUserInfo(UserInfo userInfo) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        userInfo.setUpdateUserId(userAccount.getAccountId());
        userInfo.setUpdateTm(new Date());
        return userInfoDbService.updateUserInfoByUserId(userInfo);
    }

    /**
     * 根据用户id查询用户的下属
     * @param userId 用户id
     * @param onTheJobSituation 在职状态  0在职  1离职
     * @return
     */
     /**
      *
      * @param:
      * @return:
      * @Author: baijian
      * @Date: 2021/6/9
      */
    @Override
    public DhPageInfo<UserInfo> subordinateList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String userId,String onTheJobSituation) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        String order = "create_tm desc";
        List<UserInfo> list = new ArrayList<>();
                //查询下属表   查询下属的id
        List<UserInfoSubordinate> subList = userInfoSubordinateDbService.subordinateList(userId);
        if(subList.size() != 0){
            for(UserInfoSubordinate userInfoSubordinate : subList) {
                UserInfo userInfo = userInfoDbService.subordinateList(order,userInfoSubordinate.getSubordinateId(),onTheJobSituation);
                if(!StringUtils.isBlank(userInfo.getUserId())){
                    list.add(userInfo);
                }
            }
        }

        PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(list);
        return new DhPageInfo<UserInfo>(pageInfo, list);
    }

    /***
     * @Description:插入用户信息
     * @Param: 用户信息
     * @Author: luojie
     * @Date: 2021/5/19 14:38
     */
    @Override
    public Boolean insertUserInfo(UserInfo userInfo) {
        return userInfoDbService.insertUserInfo(userInfo);
    }

    /***
     * @Description:更新用户信息
     * @Param: 用户model
     * @Author: luojie
     * @Date: 2021/5/19 14:38
     */
    @Override
    public Boolean updateUserInfo(UserInfo userInfo) {
        Boolean result = userInfoDbService.updateUserInfoByUserId(userInfo);
        return result;
    }

    /**
     * 通过accountId查询
     * @param accountId
     * @return
     */
    @Override
    public UserInfoVO selectByAccountId(String accountId) {
        return userInfoDbService.selectByAccountId(accountId);
    }

}
